package lee.study.PriorityQueue;

import java.util.Arrays;

public class TestHeap {//创建一个堆
    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
    }
    //O(log2n)
    //root-->每棵子树的开始位置
    //len--->结束位置
    public void adjustDown(int root, int len) {//向下调整
        int parent = root;
        int child = 2 * parent + 1;
        while (child < len) {
            if (child + 1 < len && this.elem[child] < this.elem[child + 1]) {//0.判断是否有左右孩子，有的话找到最大值  C表示最大值下标
                //(如果child+1等于len，则说明只有左子树，如果大于，则说明没有左右子树)
                child++;
            }
            //代码走到这里，比较孩子节点和父亲节点的大小
            if (this.elem[parent] < this.elem[child]) {//孩子节点大，进行交换
                int tmp = this.elem[parent];
                this.elem[parent] = this.elem[child];
                this.elem[child] = tmp;

                //孩子节点为父亲结点的下一层是否满足？
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }
    //O(n)
    public void creatHeap(int[] array) {//创建堆
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }
        //i:每棵子树的根节点下标
        //(this.usedSize - 1 - 1) / 2--->根据子节点n求父亲节点: (n-1)/2
        for (int i = (this.usedSize - 1 - 1) / 2; i >= 0; i--) {
            adjustDown(i, this.usedSize);
        }
    }

    public void show() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }


    //堆的插入和删除
    //1.插入
    public void push(int val) {
        //0.当前堆是否是满的
        if (isfull()) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
        //1.放到数组的最后一个位置
        this.elem[this.usedSize] = val;
        this.usedSize++;
        //2.进行调整
        adjustUp(usedSize - 1);
    }

    private boolean isfull() {
        return this.elem.length == this.usedSize;
    }

    public void adjustUp(int child) {
        int parent = (child - 1) / 2;
        while (child > 0) {
            if (this.elem[child] > this.elem[parent]) {
                int tmp = this.elem[child];
                this.elem[child] = this.elem[parent];
                this.elem[parent] = tmp;
                child = parent;
                parent = (child - 1) / 2;
                usedSize++;
            } else {
                break;
            }
        }
    }

    //2.堆的删除
    public void pop() {
        //0.是否为空
        if (isEmpty()) {
            return;
        }
        //1.最后一个元素和堆顶元素进行交换
        int tmp = this.elem[0];
        this.elem[0] = this.elem[this.usedSize - 1];
        this.elem[this.usedSize - 1] = tmp;
        this.usedSize--;
        //2.调整0号下标这棵树(向下转型)
        adjustDown(0, this.usedSize);
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    public int peek() {//拿到堆头元素
        if (isEmpty()) return -1;
        else return this.elem[0];
    }

    //堆排序
    //时间复杂度：O(n*log2n)
    //空间复杂度：O(1)
    public void sort() {
        //0.大根堆/小根堆
        int end = this.usedSize - 1;
        while (end > 0) {
            //1.交换--->0号下标与end下标
            int tmp = this.elem[end];
            this.elem[end] = this.elem[0];
            this.elem[0] = tmp;
            //2.调整
            adjustDown(0, end);
            end--;
        }
    }
}
